﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Data;
using System.Text;

using OpenNETCF.WindowsCE;

namespace MoneyTrackin
{
    public class Logic
    {
        private DataSet1 _dataSet;
        public DataSet1 DataSet
        {
            get { return _dataSet; }
            private set { _dataSet = value; }
        }
        private com.moneytrackin.www.moneytrackinAPI api = new MoneyTrackin.com.moneytrackin.www.moneytrackinAPI();
        private string dataFilePath = "transactions.xml";

        private static Logic _instance;
        public static Logic Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new Logic();
                return _instance;
            }
        }
        private Logic()
        {
            string path;
            path = System.IO.Path.GetDirectoryName(
               System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
            dataFilePath = path + @"\" + dataFilePath;
            dataFilePath = dataFilePath.Replace(@"file:\", string.Empty);

            DataSet = new DataSet1();
            if (!File.Exists(dataFilePath))
            {
                File.Create(dataFilePath).Close();
                DataSet.WriteXml(dataFilePath);
            }
            DataSet.ReadXml(dataFilePath);
        }

        public void SubmitAllTransactions()
        {
            foreach (DataRow row in DataSet.Transactions.Rows)
            {
                try
                {
                    string responce = api.insertTransaction(ConfigurationSettings.Instance.Login,
                        ConfigurationSettings.MD5(ConfigurationSettings.Instance.Password),
                        ConfigurationSettings.Instance.ProjectID,
                        row["description"].ToString(),
                        row["amount"].ToString(),
                        Convert.ToDateTime(row["date"]).ToString("yyyy-MM-dd"),
                        row["tags"].ToString(),
                        string.Empty);
                }
                catch (Exception ex)
                {
                    throw ex;
                }

            }
            DataSet.Transactions.Clear();
            DataSet.AcceptChanges();
            DataSet.WriteXml(dataFilePath);
        }

        public void TryToSubmit()
        {
            try
            {
                Logic.Instance.SubmitAllTransactions();
            }
            catch (System.Net.WebException)
            { }
        }

        public void AddTransaction(string description,
            decimal amount, DateTime date, string tags)
        {
            DataSet.Transactions.Rows.Add(description,
                amount, date, tags);
            DataSet.AcceptChanges();
            DataSet.WriteXml(dataFilePath);

            TryToSubmit();
        }
    }
}
